package com.alan.camunda.boot.util;

import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.impl.juel.SimpleContext;
import org.camunda.bpm.impl.juel.jakarta.el.ExpressionFactory;

import java.util.Map;

@Slf4j
public class JuelUtil {

    public static final ExpressionFactory factory = ExpressionFactory.newInstance();

    private SimpleContext context = new SimpleContext();

    public JuelUtil() {

    }

    public JuelUtil(Map<String, Object> variables) {
        variables.forEach((k, v) -> {
            context.setVariable(k, factory.createValueExpression(v, v.getClass()));
        });
    }

    public void setVariable(String name, Object value) {
        context.setVariable(name, factory.createValueExpression(value, value.getClass()));
    }

    public Object getValue(String expression) {
        Object value = factory.createValueExpression(context, expression, Object.class).getValue(context);
        log.info("{} -----> {}", expression, value);
        return value;
    }

}
